package com.qf.service.impl;

import com.qf.dao.CommunityMapper;
import com.qf.dao.TenementMapper;
import com.qf.dao.WorkerMapper;
import com.qf.pojo.Community;
import com.qf.pojo.Tenement;
import com.qf.pojo.Worker;
import com.qf.pojo.WorkerExample;
import com.qf.queryVo.WorkerVo;
import com.qf.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WorkerServiceImpl implements WorkerService {

    @Autowired
    private WorkerMapper workerMapper;
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private TenementMapper tenementMapper;

    @Override
    public List<Worker> findAll(WorkerVo workerVo) {
        String name = workerVo.getName();
        if (name == null) {
            name = "";
        }
        List<Integer> communityIds = new ArrayList<>();
        if (workerVo.getTenementId() != null) {
            communityIds = communityMapper.selectIdsByTenementId(workerVo.getTenementId());
        }
        workerVo.setName("%" + name + "%");
        List<Worker> workers = workerMapper.selectByConditions(workerVo, communityIds);
        return workers;
    }

    @Override
    public List<Worker> findAll() {
        List<Worker> workers = workerMapper.selectByExample(null);
        return workers;
    }

    @Override
    public Worker findById(Integer id) {
        return workerMapper.selectByPrimaryKey(id);
    }

    @Override
    public void removeOne(Integer id) {
        workerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void removeList(List<Integer> ids) {
        WorkerExample workerExample = new WorkerExample();
        WorkerExample.Criteria criteria = workerExample.createCriteria();
        criteria.andIdIn(ids);
        workerMapper.deleteByExample(workerExample);
    }

    @Override
    public void addWorker(Worker worker) {
        workerMapper.insert(worker);
    }

    @Override
    public void updateWorker(Worker worker) {
        workerMapper.updateByPrimaryKey(worker);
    }

    @Override
    public List<Community> toGetCommunity() {
        return communityMapper.selectByExample(null);
    }

    @Override
    public List<Tenement> toGetTenement() {
        return tenementMapper.selectByExample(null);
    }
}
